package com.uu.it.tracker.views;

import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;

import com.android.tracker.R;
import com.android.tracker.R.id;
import com.android.tracker.R.layout;
import com.uu.it.tracker.db.DBAdapter;
import com.uu.it.tracker.twitter.TwitterUpdater;

import android.app.Activity;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class WorkoutView  extends Activity{
	
	private String date, starttime, endtime, duration, distance, max_speed, avg_speed, id;
	private DBAdapter db = new DBAdapter(this);
	 @Override
	    protected void onCreate(Bundle savedState) {
	        super.onCreate(savedState);
	        setContentView(R.layout.workoutview);
	        
	        //get record from database
	        SharedPreferences preferences = getSharedPreferences("workouts", 0);
	        date = preferences.getString("date", null);
	        starttime = preferences.getString("starttime", null);
	        
	        db.open();
	        Cursor c = db.getHistory("date=\""+date+"\" AND starttime=\""+starttime+"\"");
	       
	        //Toast.makeText(this, c.getString(1)+" "+c.getString(2)+" "+c.getString(3), Toast.LENGTH_SHORT).show();
	        TextView tmp = (TextView)this.findViewById(R.id.Date);
	        tmp.setText(date);
	        tmp = (TextView)this.findViewById(R.id.start_time);
	        tmp.setText(starttime);
	        tmp = (TextView)this.findViewById(R.id.end_time);
	        tmp.setText(c.getString(3));
	        
	        /**
	         * get duration from startime and endtime
	         */
	        DateFormat tfmt = DateFormat.getTimeInstance(DateFormat.MEDIUM);
	        Date endTime = null, startTime = null;
	        endtime = c.getString(3);
	        try {
				 endTime = tfmt.parse(endtime);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				 startTime = tfmt.parse(starttime);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        long l = (endTime.getTime()-startTime.getTime())/1000;
	        long h = l/3600;
			long min = (l%3600)/60;
			long s = (l%3600)%60;
			
			duration = new String(h+":"+min+":"+s);
			tmp = (TextView)this.findViewById(R.id.duration);
			tmp.setText(duration);
			
			distance = c.getString(4);
			tmp = (TextView)this.findViewById(R.id.distance);
			tmp.setText(distance+" km");
			
			avg_speed = c.getString(5);
			tmp = (TextView)this.findViewById(R.id.Avg_speed);
			tmp.setText(avg_speed +" km/h");
			
			max_speed = c.getString(6);
			tmp = (TextView)this.findViewById(R.id.Max_speed);
			tmp.setText(max_speed +" km/h");
			
			id = c.getString(0);
			c.close();
			db.close();
			
			//Register twitter button
			ImageButton twitter = (ImageButton)this.findViewById(R.id.Twitter);
			twitter.setOnClickListener(twitterListener);
			
			//Register delete button
			Button delete = (Button)this.findViewById(R.id.Delete);
			delete.setOnClickListener(deleteListener);
	 }
	 
	 
	 private OnClickListener deleteListener = new OnClickListener() {

		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			db.open();
			db.deleteHistory(id);
			db.close();
			Toast.makeText(WorkoutView.this, "record deleted", Toast.LENGTH_SHORT).show();
			finish();	
		}
		 
	 };
	 /**
	  * Define actions when twitter is clicked
	  */
	 private OnClickListener twitterListener = new OnClickListener() {
		public void onClick(View v) {
			// TODO Auto-generated method stub
			SharedPreferences preferences = getSharedPreferences("login", 0);
		     final String user = preferences.getString("user", null);
		     final String passwd = preferences.getString("pass", null);
		   
		     //if user and password are not set, inform the user
		     if(user == null || passwd == null){
		    	 Toast.makeText(WorkoutView.this, "please setup username and" +
			    	 		" password", Toast.LENGTH_SHORT).show();
		    	 return;
		     }

			Thread twitterThread = new Thread(new Runnable(){
				public void run(){	 
	                TwitterUpdater tu = new TwitterUpdater("datacom3project","datacom3");
					tu.updateMessage("My workout\nDate: "+ date + "\nStart time: " 
							+ starttime + "\tEnd time: " + endtime
							+ "\nDuration: " + duration + "\tDistance: " + distance
							+ "\nAvg Speed: " + avg_speed + "\tMax Speed: " + max_speed);
				}
			});
			
			twitterThread.start();

		}
	
	};
	
}
